package cn.est.controller;

import cn.est.constants.Constants;
import cn.est.dto.ClassfyDto;
import cn.est.dto.Result;
import cn.est.service.ClassifyService;
import cn.est.utils.RedisUtil;
import cn.est.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api
@RestController
@RequestMapping("/api/home")
public class HomeController {

    @Resource
    ClassifyService classifyService;

    @Autowired
    RedisUtil redisUtil;

    @ApiOperation(value="首页的商品分类",notes = "首页的商品分类树，包含分类、品牌、型号信息 ")
    @GetMapping(value = "/classsifyTree", produces = "application/json")
    public Result<List<ClassfyDto>> list(){
        List<ClassfyDto> result= (List<ClassfyDto>) redisUtil.get(Constants.Home.CLASSFYLIST);
        if (result==null) {
            System.out.println("执行了数据库查询");
            result = classifyService.queryClassfytree();
            redisUtil.set(Constants.Home.CLASSFYLIST, result, Constants.Home.CLASSFYLIST_TIME);
        }
        return ResultUtils.returnDataSuccess(result);
    }
}
